import { Tween, Easing } from '@tweenjs/tween.js';
let tween,
  animte = false;

export function camerAnimate(camera, control, option) {
  const { to, onStart, onComplete } = option;
  const p1 = Object.values(camera.position);
  const p2 = Object.values(control.target);
  const start = [...p1, ...p2];
  const end = [...to.camera, ...to.target];
  tween = new Tween(start, false).to(end, 1500);
  tween.easing(Easing.Quadratic.InOut);
  tween.onUpdate(() => {
    if (camera) {
      camera.position.set(start[0], start[1], start[2]);
    }
    if (control) {
      control.target.set(start[3], start[4], start[5]);
    }
  });
  tween.onStart(() => {
    onStart && onStart();
  });

  tween.onComplete(() => {
    animte = false;
    onComplete && onComplete();
  });
  tween.start();
  animte = true;
}
export function cameraUpdate(time) {
  if (animte) tween.update(time);
}
